#include "mainWindow.h"

mainWindow::mainWindow()
{
	//QRect screenRect = QApplication::desktop()->screenGeometry();
	//this.setGeometry(100, 100, 800, 400);

    //按钮控件
    pushBtn1 = new QPushButton("pushBtn1");
    pushBtn2 = new QPushButton("pushBtn2");
    QSizePolicy sizePolicy = this->sizePolicy();
    sizePolicy.setHorizontalPolicy(QSizePolicy::Expanding);
    sizePolicy.setVerticalPolicy(QSizePolicy::Expanding);
    pushBtn1->setSizePolicy(sizePolicy);
    pushBtn2->setSizePolicy(sizePolicy);
    pushBtn1->setProperty("btnID", 1);
    pushBtn2->setProperty("btnID", 2);

    //多选框
    checkBox1 = new QCheckBox("读书");
    checkBox2 = new QCheckBox("写字");
    //设置三态
    checkBox1->setTristate();
    //按钮组
    checkGroup = new QButtonGroup(this);
    //设置不互斥
    checkGroup->setExclusive(false);
    //增加按钮
    checkGroup->addButton(checkBox1);
    checkGroup->addButton(checkBox2);

    //单选按钮
    radioBtn1 = new QRadioButton("男");
    radioBtn2 = new QRadioButton("女");
    radioBtn1->resize(50,50);
    radioBtn2->resize(100,50);
    //按钮组
    radioGroup = new QButtonGroup(this);
    //设置互斥
    radioGroup->setExclusive(true);
    //增加按钮
    radioGroup->addButton(radioBtn1);
    radioGroup->addButton(radioBtn2);

	QToolButton *toolBtn1 = new QToolButton;
    label1 = new QLabel("普通label");
    label2 = new QLabel("label2");
    QMovie *movie = new QMovie(":images/fire.gif");
    label2->setMovie(movie);
    movie->start();

    lcdNum = new QLCDNumber(this);
    lcdNum->setSizePolicy(sizePolicy);
    lcdNum->resize(100,50);
    // 设置能显示的位数
    lcdNum->setDigitCount(25);
    // 设置显示的模式为十进制
    lcdNum->setMode(QLCDNumber::Dec);
    // 设置显示外观
    lcdNum->setSegmentStyle(QLCDNumber::Flat);
    // 设置样式
    lcdNum->setStyleSheet("border: 1px solid green; color: green; background: silver;");

    QTimer *timer = new QTimer(this);
    // 设置定时间隔
    timer->setInterval(1000);
    connect(timer, SIGNAL(timeout()), this, SLOT(onTimeOut()));
    // 启动定时器
    timer->start();

    //下拉列表框
    comboBox = new QComboBox;
    QStringList list;
    list<<"曾老大"<<"媞子"<<"李兴华"<<"张文青";
    //添加条目
    comboBox->addItems(list);
    //添加条目
    comboBox->addItem("魏杰");


    textEdit = new QTextEdit("textEdit");
    lineEdit = new QLineEdit("lineEdit");

    spinBox = new QSpinBox;
    // 范围
    spinBox->setRange(20, 200);
    // 步长
    spinBox->setSingleStep(10);
    // 当前值
    spinBox->setValue(150);
    // 前缀
    spinBox->setPrefix("$ ");
    // 后缀
    spinBox->setSuffix(" %");
    // 开启循环
    spinBox->setWrapping(true);

    doubleSpinBox = new QDoubleSpinBox;
    //范围
    doubleSpinBox->setRange(0, 20);
    //精度
    doubleSpinBox->setDecimals(3);
    //步长
    doubleSpinBox->setSingleStep(0.005);

    progressBar = new QProgressBar;
    //打开资源文件
    QFile file(":/style/progressBar.qss");
    if (file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"hehe";
        QString str = QString::fromLatin1(file.readAll());
        //给某个窗体设置样式表，progressBar为某个窗体对象
        progressBar->setStyleSheet(str);
        file.close();
    }
    //水平方向
    progressBar->setOrientation(Qt::Horizontal);
    //最小值
    progressBar->setMinimum(0);
    //最大值
    progressBar->setMaximum(4800);
    //当前进度
    progressBar->setValue(2000);
    double dProgress = (progressBar->value() - progressBar->minimum()) * 100.0
                    / (progressBar->maximum() - progressBar->minimum());
    progressBar->setFormat(QString::fromLocal8Bit("当前进度为：%1%").arg(QString::number(dProgress, 'f', 1)));
    //对齐方式
    progressBar->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    dateEdit = new QDateEdit(QDate::currentDate(), this);
    timeEdit = new QTimeEdit(QTime::currentTime(), this);
    dateTimeEdit = new QDateTimeEdit(QDateTime::currentDateTime(), this);
    //设置显示格式
    dateEdit->setDisplayFormat("yyyy/MM/dd HH:mm:ss");
    timeEdit->setDisplayFormat("yyyy/MM/dd HH:mm:ss");
    dateTimeEdit->setDisplayFormat("yyyy/MM/dd HH:mm:ss");
    // 正确
    //dateEdit->setDisplayFormat("yyyy/MM/dd");
    //timeEdit->setDisplayFormat("HH:mm:ss");
    // 设置日期时间
    dateEdit->setDateTime(QDateTime::currentDateTime());
    timeEdit->setDateTime(QDateTime::currentDateTime());

    dateTimeEdit->setMinimumDate(QDate::currentDate().addDays(-365));  // -365天
    dateTimeEdit->setMaximumDate(QDate::currentDate().addDays(365));  // +365天


	QDateTimeEdit *dateTimeEdit = new QDateTimeEdit;
	QCalendarWidget *calendarWidget = new QCalendarWidget;
	QListView *listView = new QListView;
	QTableView *tableView = new QTableView;
	QTreeView *treeView = new QTreeView;
	//QWebEngineView *webView = new QWebEngineView;
	QFont *font;
	QPalette *palette;

	//布局
	QVBoxLayout *vLay = new QVBoxLayout();
	QHBoxLayout *hLay = new QHBoxLayout();
	QVBoxLayout *vLay1 = new QVBoxLayout();
	QVBoxLayout *vLay2 = new QVBoxLayout();
	QVBoxLayout *vLay3 = new QVBoxLayout();
	QVBoxLayout *vLay4 = new QVBoxLayout();
	vLay1->addWidget(pushBtn1);
	vLay1->addWidget(pushBtn2);
    vLay2->addWidget(radioBtn1);
    vLay2->addWidget(radioBtn2);
    vLay3->addWidget(checkBox1);
    vLay3->addWidget(checkBox2);
    vLay4->addWidget(label1);
    vLay4->addWidget(label2);
	hLay->addLayout(vLay1);
	hLay->addLayout(vLay2);
	hLay->addLayout(vLay3);
	hLay->addLayout(vLay4);
    hLay->addWidget(dateTimeEdit);
	vLay->addLayout(hLay);
    vLay->addWidget(textEdit);
	//整体布局
    QWidget *pWidget = new QWidget(this);
    pWidget->setLayout(vLay);
    setCentralWidget(pWidget);
    //this->setCentralWidget((QWidget *)pushBtn1);
	//连接信号和槽
    connect(pushBtn1, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(pushBtn2, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(radioGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(radioBtnSlot(QAbstractButton*)));
    connect(checkGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(checkBoxSlot(QAbstractButton*)));
    connect(checkBox1, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int)));
    connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxChanged(int)));
    connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(spinBoxChangedSlot(int)));
    connect(spinBox, SIGNAL(valueChanged(QString)), this, SLOT(spinBoxChangedSlot(QString)));
    connect(doubleSpinBox, SIGNAL(valueChanged(QString)), this, SLOT(spinBoxChangedSlot(QString)));
    connect(dateTimeEdit, SIGNAL(dateChanged(QDate)), this, SLOT(onDateChangedSlot(QDate)));
    connect(dateTimeEdit, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(onDateTimeChangedSlot(QDateTime)));
    connect(dateTimeEdit, SIGNAL(timeChanged(QTime)), this, SLOT(onTimeChangedSlot(QTime)));

	/*
	QTabWidget *tab = new QTabWidget();
	QWidget *widget1 = new QWidget();
	QWidget *widget2 = new QWidget();
	widget1->setLayout(gLay);
	widget2->setLayout(gLay);
	tab->addTab(widget1, "h"); //添加widget，以及给它起名
	tab->addTab(widget2, "v");
	setCentralWidget(tab);
	*/
	//pushBtn->setText("push");
    //label2->setFixedSize(150, 150);
    //lcdNum->setFixedSize(200,50);
}
void mainWindow::pushBtnInit()
{
	//pushBtn->setText("push");
}
void mainWindow::pushBtnSlot()
{
    QPushButton *btn = (QPushButton *)sender();
    int id = btn->property("btnID").toInt();
    if (id==1)
    {
        pushBtn2->setIcon(QIcon(":/images/open.png"));
        pushBtn2->setText("有图片的按钮");
        textEdit->setText(tr("你点击了普通按钮"));
    }
    else
        textEdit->setText(tr("你点击了图片按钮"));
}

void mainWindow::checkBoxChanged(int state)
{
    if (state == Qt::Checked) // "选中"
    {
        textEdit->setText("Checked");
    }
    else if(state == Qt::PartiallyChecked) // "半选"
    {
        textEdit->setText("PartiallyChecked");
    }
    else // 未选中 - Qt::Unchecked
    {
        textEdit->setText("Unchecked");
    }
}

void mainWindow::checkBoxSlot(QAbstractButton *button)
{
    // 当前点击的按钮
    QString str =  QString("Clicked Button : %1").arg(button->text());
    textEdit->setText(str);
    // 遍历按钮，获取选中状态
    QList<QAbstractButton*> list = checkGroup->buttons();
    foreach (QAbstractButton *pButton, list)
    {
        QString strStatus = pButton->isChecked() ? "Checked" : "Unchecked";
        qDebug() << QString("Button : %1 is %2").arg(pButton->text()).arg(strStatus);
    }
}
void mainWindow::radioBtnSlot(QAbstractButton *button)
{
    // 当前点击的按钮
    qDebug() << QString("Clicked Button : %1").arg(button->text());
}

void mainWindow::onTimeOut()
{
    // 获取系统当前时间
    QDateTime dateTime = QDateTime::currentDateTime();
    // 显示的内容
    lcdNum->display(dateTime.toString("yyyy-MM-dd HH:mm:ss.zzz"));
}

void mainWindow::comboBoxChanged(int index)
{
    textEdit->setText(comboBox->itemText(index));
}

void mainWindow::toolBtnSlot()
{
	
}
void mainWindow::comboBoxSlot()
{
	
}
void mainWindow::textEditSlot()
{
	
}
void mainWindow::spinBoxChangedSlot(int value)
{
    qDebug()<<spinBox->value();
    qDebug()<<value;
}
void mainWindow::spinBoxChangedSlot(QString str)
{
    qDebug()<<spinBox->value();
    qDebug()<<spinBox->text();
    qDebug()<<str;
}
void mainWindow::doubleSpinBoxSlot()
{
	
}
void mainWindow::progressBarSlot()
{
	
}
void mainWindow::dateEditSlot()
{
	
}
void mainWindow::dateTimeEditSlot()
{
	
}
void mainWindow::calenderWidgetSlot()
{
	
}
// 日期发生改变时执行
void mainWindow::onDateChangedSlot(const QDate &date)
{
    qDebug() << "QDate : " << date;
}

// 无论日期还是时间发生改变，都会执行
void mainWindow::onDateTimeChangedSlot(const QDateTime &dateTime)
{
    qDebug() << "QDateTime : " << dateTime;
}

// 时间发生改变时执行
void mainWindow::onTimeChangedSlot(const QTime &time)
{
    qDebug() << "QTime : " << time;
}
void mainWindow::listViewSlot()
{
	
}
void mainWindow::tableViewSlot()
{
	
}
void mainWindow::treeViewSlot()
{
	
}
void mainWindow::webViewSlot()
{
	
}
